package com.android.questions.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;

import com.android.base.util.ActivityClickUtil;
import com.android.base.util.DQLog;
import com.android.base.util.DisplayUtil;
import com.android.base.util.SystemUtil;
import com.android.questions.R;
import com.android.questions.config.AppConfig;
import com.android.questions.databinding.DialogBannedBinding;

/**
 * FileName: DialogBanned
 * Author: admin
 * Date: 2021/12/7 11:09
 * Description:
 */
public class DialogBanned extends Dialog implements View.OnClickListener {

    private DialogBannedBinding bannedBinding;
    private final Activity activity;
    private OnClickLister clickLister;
    private String title = "检测到账号存在安全问题";
    private int endTime = 6;

    public DialogBanned(Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bannedBinding = DialogBannedBinding.inflate(activity.getLayoutInflater());
        setContentView(bannedBinding.getRoot());

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        getWindow().setWindowAnimations(R.style.dialog_answer_correct);

        setCanceledOnTouchOutside(false);
        setCancelable(false);
        initView();
    }

    private void initView() {
        ActivityClickUtil.getInstance().clear();
        bannedBinding.ivOk.setOnClickListener(this);
        bannedBinding.ivJoinQqGroup.setOnClickListener(this);

        bannedBinding.tvTitle.setText(title);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                DQLog.e("entTime:" + endTime);
                bannedBinding.tvExitTime.setText("即将退出:" + Math.max(endTime, 1));
                if (endTime < 1) {
                    dismiss();
                    if (clickLister != null) {
                        clickLister.onClose();
                    }
                } else {
                    endTime--;
                    new Handler().postDelayed(this, 1000);
                }
            }
        }, 10);
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        if (v.getId() == R.id.iv_ok) {
            dismiss();
            if (this.clickLister != null) {
                this.clickLister.onClose();
            }
        } else if (v.getId() == R.id.iv_join_qq_group) {
            SystemUtil.joinQQGroup(activity, AppConfig.QQ_GROUP_KEY);
        }
    }

    public DialogBanned setTitle(String title) {
        if (!title.isEmpty()) {
            this.title = title;
        }
        return this;
    }

    public DialogBanned setClickLister(OnClickLister onClickLister) {
        this.clickLister = onClickLister;
        return this;
    }

    public interface OnClickLister {
        void onClose();
    }
}
